package com.jpcc.vw.onlineRadio.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.db.DownloadTrackDbBean;
import com.jpcc.vw.onlineRadio.model.action.DeleteDownloadAlbumAction;
import com.jpcc.vw.onlineRadio.service.AlbumRadioAudioPlayer;
import com.jpcc.vw.onlineRadio.utils.ViewHolderUtils;
import com.jpcc.vw.onlineRadio.widget.Indicator;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class AlbumDetailDownloadAdapter extends BaseAdapter {
    private List<DownloadTrackDbBean> datas = new ArrayList<>();
    private Context cx;

    public AlbumDetailDownloadAdapter(List<DownloadTrackDbBean> sources, Context context) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            cx = context;
        }
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(cx, R.layout.adapter_download_track_item, null);
        }
        initViews(convertView, position);
        return convertView;
    }

    public void setDatas(List<DownloadTrackDbBean> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }

    private void initViews(View convertView, int position) {
        TextView tv_item_count = ViewHolderUtils.get(convertView, R.id.tv_item_count);
        TextView tv_title = ViewHolderUtils.get(convertView, R.id.tv_title);
        ImageView imv_delete = ViewHolderUtils.get(convertView, R.id.imv_delete);
//        Indicator imv_cover=ViewHolderUtils.get(convertView,R.id.imv_cover);
        ImageView imv_play_status=ViewHolderUtils.get(convertView,R.id.imv_play_status);
        RelativeLayout layout_top=ViewHolderUtils.get(convertView,R.id.layout_top);
        imv_delete.setVisibility(View.VISIBLE);
        tv_item_count.setText(position+1 + "");
        tv_title.setText(datas.get(position).getTrackName());


        String bgColor = "#404040";
        if (Constants.isRelease) {
            try {
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(cx).isHmiAtmosphereLampOn();
                String color = AtmosphereLampManager.getInstance(cx).getHmiAtmosphereLampColor();
                if (atmosphereStatus) {
                    if (!TextUtils.isEmpty(color)) {
                        bgColor = color;
                    }
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }





        if((AlbumRadioAudioPlayer.get().getPlayMusic()!=null)&&(datas.get(position).getTrackId()== AlbumRadioAudioPlayer.get().getPlayMusic().getTrackId())){
//            imv_cover.setVisibility(View.VISIBLE);
            imv_play_status.setVisibility(View.VISIBLE);
//            imv_cover.setImageResource(R.drawable.playing);
//            layout_top.setBackgroundColor(Color.parseColor(bgColor));
            layout_top.setBackgroundColor(Color.parseColor("#000000"));


            if(AlbumRadioAudioPlayer.get().isPlaying()){
//                imv_cover.setIndicatorPause(true);
                imv_play_status.setImageResource(R.drawable.ic_play_in_list);
            }
            else{
//                imv_cover.setIndicatorPause(false);
                imv_play_status.setImageResource(R.drawable.ic_pause_in_list);
            }
        }
        else{
//            imv_cover.setVisibility(View.GONE);
            imv_play_status.setVisibility(View.GONE);
            layout_top.setBackgroundColor(Color.parseColor("#000000"));
        }
        imv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeleteDownloadAlbumAction deleteAction = new DeleteDownloadAlbumAction();
                deleteAction.setAlbumId(datas.get(position).getAlbumId());
                deleteAction.setTrackId(datas.get(position).getTrackId());
                EventBus.getDefault().post(deleteAction);
            }
        });


        if(AlbumRadioAudioPlayer.get().isPlaying()||AlbumRadioAudioPlayer.get().isPreparing()){
//            imv_cover.setIndicatorPause(true);
            imv_play_status.setImageResource(R.drawable.ic_play_in_list);
        }
        else{
//            imv_cover.setIndicatorPause(false);
            imv_play_status.setImageResource(R.drawable.ic_pause_in_list);
        }


    }
}
